#include "cv.h"
#include "highgui.h"
#include <string>
using namespace std;

/**
 * simple smooth
 * @param: IplImage* image
 * @return: 
 */
void simpleTr(IplImage* image){
	string windowNameIn = "in";
	string windowNameOut = "out";
	cvNamedWindow(windowNameIn.data());
	cvNamedWindow(windowNameOut.data());
	//show the input image
	cvShowImage(windowNameIn.data(), image);

	//show smooth out
	IplImage* imageOut = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 3);
	//do smoothing
	cvSmooth(image, imageOut, CV_GAUSSIAN, 3, 3);
	cvShowImage(windowNameOut.data(), imageOut);

	//be tidy
	cvReleaseImage(&imageOut);

	cvWaitKey(0);
	cvDestroyWindow(windowNameIn.data());
	cvDestroyWindow(windowNameOut.data());
}

/**
 * Zoom function
 * @param: IplImage* image
 * @return: 
 */
void Zoom(IplImage *image, int filter = IPL_GAUSSIAN_5x5){
	string windowNameIn = "in";
	string windowNameOut = "out";
	cvNamedWindow(windowNameIn.data());
	cvNamedWindow(windowNameOut.data());
	//show the input image
	cvShowImage(windowNameIn.data(), image);

	//make sure input image is divisible by two
	//assert(image->width%2 == 0 && image->height%2 ==0);
	
	//create new image
	IplImage *imageOut = cvCreateImage(cvSize(image->width/2, image->height/2), image->depth, image->nChannels);
	cvPyrDown(image, imageOut);

	//show imageOut
	cvShowImage(windowNameOut.data(), imageOut);

	//release
	cvReleaseImage(&imageOut);

	cvWaitKey(0);
	cvDestroyWindow(windowNameIn.data());
	cvDestroyWindow(windowNameOut.data());
}

/**
 * Canny Edge Detection
 * @param: 
 * @return: 
 */
void doCanny(IplImage* image, double lowThread, double highThread, double aperture){
	string windowNameIn = "in";
	string windowNameOut = "out";
	cvNamedWindow(windowNameIn.data());
	cvNamedWindow(windowNameOut.data());
	//show the input image
	cvShowImage(windowNameIn.data(), image);

	//canny only handles gray scale images
	if (image->nChannels != 1) {
		return;
	}

	IplImage* imageOut = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	cvCanny(image, imageOut, lowThread, highThread, aperture);

	//show imageOut
	cvShowImage(windowNameOut.data(), imageOut);

	//release
	cvReleaseImage(&imageOut);

	cvWaitKey(0);
	cvDestroyWindow(windowNameIn.data());
	cvDestroyWindow(windowNameOut.data());
}

/**
 * main
 * @param: 
 * @return: 
 */
void main(){
	string filePath = "jcy.jpg";
	IplImage* image = cvLoadImage(filePath.data());
	//smooth
	//simpleTr(image);

	//zoom
	//Zoom(image);

	//canny
	IplImage* image2 = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);
	cvCvtColor(image, image2, CV_RGB2GRAY);
	doCanny(image2, 1, 1, 3);
	cvReleaseImage(&image);
}
